<?php

namespace app\web\controller;

use app\common\model\goods\Goods;
use app\common\controller\Frontend;
use app\common\model\goods\GoodsHouseStock;
use app\common\model\supplier\Brands;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use app\common\model\goods\Cate;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {


        $where = [];
        $keywords = input('keywords');
        if(!empty($keywords)){
            $where['title|specs'] = array('like','%'.$keywords.'%');
        }
        $goodmodel = new Goods();
        $list = $goodmodel
            ->where($where)
            ->with('brands')
            ->order('weigh','desc')
            ->limit(0,10)
            ->select();
        $total = $goodmodel->where($where)->count();
        foreach ($list as $row) {
            $row->visible(['id','title','place_origin','number','unit','createtime','status','price','specs','stock','new_cost_price','weigh']);
            $row->visible(['brands']);
            $row->getRelation('brands')->visible(['name']);
        }

        $list = collection($list)->toArray();
        //获取默认仓库或者所属仓库
        if(session('admin')['storehouse_id'] >0){
            $hrow = Db::name('storehouse')->where(array('id'=>session('admin')['storehouse_id']))->find();
        }else{
            $hrow = Db::name('storehouse')->where(array('is_default'=>1))->find();
        }

        $numbers = [];
        $ids = [];
        foreach ($list as &$v){
            $ids[] = $v['id'];
            $numbers[] = $v['number'];
            $house = new GoodsHouseStock();
            $v['stock'] = $house->getstock($v['id'],$hrow['id']);
        }
        $prices = $this->getprice(implode(',',$numbers),$hrow['code']);

        $carts = Db::name('sales_cart')->where(array('admin_id'=>session('admin')['id'],'goods_id'=>array('in',$ids)))->select();
        foreach ($list as &$v){
            $v['price'] = 0;
            $v['image'] = $this->thisurl.'/assets/web/images/zanwu.png';
            $v['num'] = 0;
            foreach ($carts as $value){
                if($value['goods_id'] == $v['id']){
                    $v['num'] = $value['num'];
                }
            }
        }

        $this->assign('keywords',$keywords);
        $this->assign('list',$list);
        $this->assign('total',$total);
        $this->assign('purl','index');
        return $this->view->fetch();
    }
    public function getgoods(){
        $page = input('page/d',1);
        $pagesize = input('pagesize/d',10);
        $where = [];
        $keywords = input('keywords');
        if(!empty($keywords)){
            $where['title'] = array('like','%'.$keywords.'%');
        }
        $goodmodel = new Goods();
        $list = $goodmodel->where($where)
            ->with('brands')
            ->order('weigh','desc')
            ->limit($page*$pagesize,$pagesize)
            ->select();

        foreach ($list as $row) {
            $row->visible(['id','title','place_origin','number','unit','createtime','status','price','specs','stock','new_cost_price','weigh']);
            $row->visible(['brands']);
            $row->getRelation('brands')->visible(['name']);
        }
        $list = collection($list)->toArray();
        //获取默认仓库或者所属仓库
        if(session('admin')['storehouse_id'] >0){
            $hrow = Db::name('storehouse')->where(array('id'=>session('admin')['storehouse_id']))->find();
        }else{
            $hrow = Db::name('storehouse')->where(array('is_default'=>1))->find();
        }
        $numbers = [];
        $ids = [];
        foreach ($list as &$v){
            $ids[] = $v['id'];
            $numbers[] = $v['number'];
            $house = new GoodsHouseStock();
            $v['stock'] = $house->getstock($v['id'],$hrow['id']);
        }
        $prices = $this->getprice(implode(',',$numbers),$hrow['code']);
        $carts = Db::name('sales_cart')->where(array('admin_id'=>session('admin')['id'],'goods_id'=>array('in',$ids)))->select();

        foreach ($list as &$v){
            $v['price'] = 0;
            $v['image'] = $this->thisurl.'/assets/web/images/zanwu.png';
            foreach ($carts as $value){
                if($value['goods_id'] == $v['id']){
                    $v['num'] = $value['num'];
                }
            }
        }


        $this->success('w','',$list);
    }
}
